package com.example.administrator.pandanotes;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by 于博 on 2018-07-14.
 * 统计界面，显示每本书的笔记数量
 */
public class NoteInfoActivity extends AppCompatActivity {
    //变量***********************************************************************
    //private List<String> listUsers = new ArrayList<>();
    private List<String> listNoteNums = new ArrayList<>();
    private List<String> listAllBooks = new ArrayList<>();
    //private List<String> listBookNotes = new ArrayList<>();
    private ListView lVNoteNum = null;
    private ListView lVAllBooks = null;
    //private ListView lVBookNotes = null;
    User theUser = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_info);
        //初始化及绑定****************************************************************
        //lVAllUserNames = (ListView) findViewById(R.id.lVAllUsers);
        lVNoteNum = (ListView) findViewById(R.id.lVNotesNum);
        lVAllBooks = (ListView)findViewById(R.id.lVAllBooks);
        //lVBookNotes = (ListView)findViewById(R.id.lVBookNotes);
        init();
        //ArrayAdapter<String> adapterUsers = new ArrayAdapter<String>(NoteInfoActivity.this,android.R.layout.simple_list_item_1,listUsers);
        ArrayAdapter<String> adapterNotesNum  = new ArrayAdapter<String>(NoteInfoActivity.this,android.R.layout.simple_list_item_1,listNoteNums);
        ArrayAdapter<String> adapterAllBooks = new ArrayAdapter<String>(NoteInfoActivity.this,android.R.layout.simple_list_item_1,listAllBooks);
        //ArrayAdapter<String> adapterBookNotes = new ArrayAdapter<String>(NoteInfoActivity.this,android.R.layout.simple_list_item_1,listBookNotes);
        //lVAllUserNames.setAdapter(adapterUsers);
        lVNoteNum.setAdapter(adapterNotesNum);
        lVAllBooks.setAdapter(adapterAllBooks);
        //lVBookNotes.setAdapter(adapterBookNotes);
        try {
            FileReader r = new FileReader(Environment.getExternalStorageDirectory() + "/pandanotescookies.txt");
            BufferedReader reader = new BufferedReader(r);
            String id = reader.readLine();
            Log.d("UserName:",id);
            if(id != null && (!id.equals(""))){
                List<User> users = DataSupport.where("user_id = ?",id).find(User.class);
                theUser = users.get(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //初始化函数，初始化书籍和笔记数量
    private void init()
    {
        //List<User> users = DataSupport.findAll(User.class); //得到用户表所有数据
        //listUsers.clear();//清空
        listNoteNums.clear();
        listAllBooks.clear();
        //listBookNotes.clear();
        //for(User user:users)                                //每个数据项进行ListView的映射
        //{
            //listUsers.add(user.getUser_name());             //添加用户
            //List<Note> notes = DataSupport.where("userid=?",user.getUser_id()).find(Note.class);//找到用户的所有笔记
            //listNoteNums.add(String.valueOf(notes.size()));//添加笔记数量
        //}
        List<Books> bookses = DataSupport.findAll(Books.class);
        for (Books books:bookses)
        {
            listAllBooks.add("                    "+books.getBook_name()+"                         ");//添加书
            List<Note> notes = DataSupport.where("book_name=?",String.valueOf(books.getBook_name())).find(Note.class);//找到该书笔记
            int i = notes.size();
            listNoteNums.add("                    "+String.valueOf(i)+"              ");
            //while (i!=0){listAllBooks.add(books.getBook_name());i--;}
            //for (Note note:notes)
            //{
                //listBookNotes.add(note.getNote_content());
            //}
        }
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
